#!/usr/bin/perl -w
# 
# @param $1 install home
# @param $2 delimiter
# @param $3 host
# @param $4 port
# @param $5 username
# @param $6 password
#
# -- 07/07/2009 Created by Zhixin Li
# -- Used to collect /etc/mytab
# --        basedir|conffile|port|startup|status  
# -- if output as 0|0|port|0|0  means something is wrong 
# status: 0  error  without /etc/mytab, wrong data, duplicate port
#         1  normal
#
use strict;

my ($MYSQL_HOME,$DELIM,$HOST,$PORT,$USER,$PASS) = @ARGV;
my $PARAM="";
$PARAM="$PARAM -h\"$HOST\"" if (defined($HOST) && $HOST ne "");
$PARAM="$PARAM -P\"$PORT\"" if (defined($PORT) && $PORT ne "");
$PARAM="$PARAM -u\"$USER\"" if (defined($USER) && $USER ne "");
$PARAM="$PARAM -p\"$PASS\"" if (defined($PASS) && $PASS ne "");
my $MYSQL="$MYSQL_HOME/bin/mysql";
my $MYSQLADMIN="$MYSQL_HOME/bin/mysqladmin";
if ($^O =~ m/MSWin32/) {
  $MYSQL =~ s/\//\\/g;
  $MYSQLADMIN =~ s/\//\\/g;
}
# Check if mysqld is alive
my $retmsg=`$MYSQLADMIN $PARAM ping 2>&1`;
chomp($retmsg);
if ( "$retmsg" ne "mysqld is alive" ) {
    print "em_error=Down${DELIM}Down${DELIM}Down${DELIM}Down${DELIM}Down\n";
    exit 1;
}
my $cf = '/etc/mytab';
if (! -e $cf || ! -s $cf) {            #  check if /etc/mytab exists or is NULL
     print "em_result=0${DELIM}0${DELIM}${PORT}${DELIM}0${DELIM}0\n";
     exit 0;
}
my $SQL1="show variables like 'pid_file'";
my $SQL2="show variables like 'datadir'";
my $SQL3="show variables like 'basedir'";
my $SQL4="show variables like 'port'";
my $m_file=`$MYSQL -N -s $PARAM --batch -e "$SQL1"`;
my $m_dir=`$MYSQL -N -s $PARAM --batch -e "$SQL2"`;
my $m_base=`$MYSQL -N -s $PARAM --batch -e "$SQL3"`;
my $m_port=`$MYSQL -N -s $PARAM --batch -e "$SQL4"`;
my $pidfile = substr($m_file,index($m_file,"\t")+1,length($m_file)-index($m_file,"\t")+1);
my $datadir = substr($m_dir,index($m_dir,"\t")+1,length($m_dir)-index($m_dir,"\t")+1);
my $basedir = substr($m_base,index($m_base,"\t")+1,length($m_base)-index($m_base,"\t")+1);
my $myport = substr($m_port,index($m_port,"\t")+1,length($m_port)-index($m_port,"\t")+1);
my $ostype = `/bin/uname`;
my $mypid;
my $proc;
chomp($pidfile);
chomp($datadir);
chomp($basedir);
chomp($myport);
my @contents = `/bin/cat $cf|/bin/grep -v "#"`;
open (FILE, "$pidfile") or die "Couldn't open $pidfile for reading: $!\n";    # get mysqld pid
$/=""; 
$mypid = <FILE>; 
close(FILE); 
$/="\n";
chomp($mypid);
chomp($ostype);
chomp(@contents);
my $x;
my $y;
my @z;
my @fields;
my $val0;
my $val1;
my $val2;
my $val3;
my $key = 0;
my $rownum = 0;
for (@contents) {
    @fields = split (/[ \t]+/, $_);
    if ("$fields[2]" eq "$myport") { # compare the third row value with mysqld port
       $key = 1;
       $val0 = $fields[0];
       $val1 = $fields[1];
       $val2 = $fields[2];
       $val3 = $fields[3];
       $rownum++;
       if ("$fields[0]" ne "$basedir") {  # compare the first row value with mysqld basedir
          print "em_result=0${DELIM}0${DELIM}${val2}${DELIM}0${DELIM}0\n";
          exit 0;
       }
    }
    if ($rownum > 1) {
          print "em_result=0${DELIM}0${DELIM}${val2}${DELIM}0${DELIM}0\n";
          exit 0;
    }
}
if ( $key == 0) {       # if the third row value is not equal to mysqld port then $key == 0
   print "em_result=0${DELIM}0${DELIM}${val2}${DELIM}0${DELIM}0\n";
   exit 0;
}

if ($ostype eq 'SunOS') {
   $proc =`/usr/ucb/ps awwx|/bin/grep -v grep|/bin/grep mysqld|/bin/grep -w "$mypid "`; 
   $x = substr($proc,index($proc,"/"),length($proc));              # get process command
   $y = grep(/--defaults-file/,$x);                                # check if mysqld start with --defaults-file option
} else {
   $proc =`/bin/ps awwx|/bin/grep -v grep|/bin/grep mysqld|/bin/grep -w "$mypid "`;
   $x = substr($proc,index($proc,"/"),length($proc));
   $y = grep(/--defaults-file=/,$x);
}
if ( $y == 1) {
   @z = split (/ +/, $x);
   foreach (@z) {
     if (/^--defaults-file/) {
        chomp($_);
        @fields = split(/=/,$_);
        my $cffile = $fields[1];
        if ("$val1" eq "$cffile") {
           print "em_result=${val0}${DELIM}${val1}${DELIM}${val2}${DELIM}${val3}${DELIM}1\n";
           exit 0;
        } else {
           print "em_result=0${DELIM}0${DELIM}${val2}${DELIM}0${DELIM}0\n";
           exit 0;
        }
     }
   }
} else {
   if ("$val1" eq "/etc/my.cnf") {
      print "em_result=${val0}${DELIM}${val1}${DELIM}${val2}${DELIM}${val3}${DELIM}1\n";
      exit 0;
   } else {
      print "em_result=0${DELIM}0${DELIM}${val2}${DELIM}0${DELIM}0\n";
      exit 0;
   } 
}
